<?php
  defined('_JEXEC') or die();
  jimport('joomla.application.component.model');
  class ConferenceTeamModelConferenceTeam extends JModel
  {     
    var $_name = "conferenceteam";
    var $_table = "#__pbny_conference_teams";
    
    function renew_teams($conference_id, $team_ids){
      
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           ' WHERE conference_id='.$conference_id;
      
      $remaining_team_ids     = array();
      $conference_teams = $this->_get($sql);      
      foreach($conference_teams as $conference_team){
      
        if(!in_array($conference_team->team_id, $team_ids)){        
          $sql = 'DELETE FROM '.$this->_table.
               ' WHERE conference_id='.$conference_id.
               ' AND team_id='.$conference_team->team_id;
          $this->_execute($sql);          
        }
        else
          array_push($remaining_team_ids, $conference_team->team_id);
      }
      
      
      foreach($team_ids as $team_id){
      
        if(!in_array($team_id, $remaining_team_ids)){
          $data = array('conference_id' => $conference_id,
                  'team_id'       => $team_id);
          $this->save($data);  
        }
      
      }
      
    }
    
    function get_by_conference_id($conference_id){
      
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           ' WHERE conference_id='.$conference_id;
      return $this->_get($sql);
      
    }
    
    function get_teams_by_conference_id($conference_id){
      
      $team = $this->getInstance('team', 'teammodel');
      
      $cts   = $this->get_by_conference_id($conference_id);
      $result = array();
      if(!empty($cts))
        foreach($cts as $ct)
          array_push($result, $team->get($ct->team_id));
      return $result;
      
    }
    
    function __construct()
    {
      parent::__construct();
   
      global $mainframe;
   
      $filter_order     = $mainframe->getUserStateFromRequest($this->_name . 'filter_order', 'filter_order', 'id', 'cmd');
      $filter_order_Dir = $mainframe->getUserStateFromRequest($this->_name . 'filter_order_Dir', 'filter_order_Dir', 'asc', 'word');
   
      $this->setState($this->_name . 'filter_order', $filter_order);
      $this->setState($this->_name . 'filter_order_Dir', $filter_order_Dir);
    }
    
    function _orderBy()
    {
      global $mainframe;

      $orderby = '';
      $filter_order     = $this->getState($this->_name . 'filter_order');
      $filter_order_Dir = $this->getState($this->_name . 'filter_order_Dir');

      if(!empty($filter_order) && !empty($filter_order_Dir))
      {
        $orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
      }

      return $orderby;
    }    

    function get($id = null)
    {      
      $db    = $this->getDBO();
      $where = null;
      if($id)
        $where = ' WHERE id = ' . $id;
            
      $db->setQuery(
        'SELECT
          *
        FROM
          ' . $this->_table . $where . $this->_orderBy()
      );
      
      $result = $db->loadObjectList();
     
      if ($result === null)
        JError::raiseError(500, 'Error reading db' .  $this->getDBO()->getErrorMsg());
        
      if($id && count($result) > 0)
        return $result[0];
      elseif(count($result) > 0)
        return $result;
      else
        return null;
    }
    
    function save($data)
    {
      $row =& $this->getTable($this->_name);
      
      if (!$row->bind($data)){
        JError::raiseError(500, 'Error binding data');
      }
      
      if (!$row->check()){
        JError::raiseError(500, 'Invalid data');                           
      }
      
      if (!$row->store()){
        $errorMessage = $row->getError();
        JError::raiseError(500, 'Error binding data: '.$errorMessage);                           
      }
      
      return $row;
    }
    
    function delete($ids)
    {
      $query = "DELETE FROM " . $this->_table . " WHERE id IN (" . implode(',', $ids) . ")";
      $db = $this->getDBO();
            $db->setQuery($query);
            if (!$db->query())
      {
                $errorMessage = $this->getDBO()->getErrorMsg();
                JError::raiseError(500, 'Error deleting: '.$errorMessage);  
            }   
    }
    
    protected function _get($sql)
    {
      $db    = $this->getDBO();
      $db->setQuery($sql);
      
      $result = $db->loadObjectList();
     
      if ($result === null)
        JError::raiseError(500, 'Error reading db' .  $this->getDBO()->getErrorMsg());
        
      if(count($result) > 0)
        return $result;
      else
        return null;
    }
    
    protected function _execute($sql){
        
      $db = $this->getDBO();
            $db->setQuery($sql);
            if (!$db->query())
      {
                $errorMessage = $this->getDBO()->getErrorMsg();
                JError::raiseError(500, 'Error deleting: '.$errorMessage);  
            }   
    }
  }
?>
